
Imports System.ComponentModel
Imports System.Globalization
Imports System.Reflection

Imports DataAccessLayer
Imports Microsoft.ApplicationBlocks.Data

Imports System.Data.SqlClient

Public Class frmAddIf

    Private Sub frmAddIf_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getFaculty()
        getMajor()


    End Sub

    Private Function GetConnection() As SqlClient.SqlConnection
        Dim connection As New SqlClient.SqlConnection

        Try
            Dim sConnectionString As String = Transfer.My.MySettings.Default.transferConnectionString.ToString()
            connection = New SqlClient.SqlConnection(sConnectionString)
            connection.Open()

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

        Return connection
    End Function

    Private Sub getFaculty()
        'SqlDataReader that will hold the returned results		
        Dim dr As SqlClient.SqlDataReader = Nothing

        'SqlConnection that will be used to execute the sql commands
        Dim connection As SqlClient.SqlConnection = Nothing

        Try
            Try
                connection = GetConnection()
            Catch ex As Exception
                MessageBox.Show("The connection with the database can't be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try

            Dim dsFaculty As New DataSet
            dsFaculty = SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, "sGetFaculty", Nothing)

            Me.CmbFac.DataSource = dsFaculty.Tables(0)
            Me.CmbFac.DisplayMember = "FacDes"
            Me.CmbFac.ValueMember = "FACCODE"

        Catch ex As Exception
            Dim errMessage As String = ""
            Dim tempException As Exception = ex

            While (Not tempException Is Nothing)
                errMessage += tempException.Message + Environment.NewLine + Environment.NewLine
                tempException = tempException.InnerException
            End While

            MessageBox.Show(String.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}" _
              + Environment.NewLine + "This test requires some modifications to the Transfer database.", errMessage), _
              "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not dr Is Nothing Then
                CType(dr, IDisposable).Dispose()
            End If

            If Not connection Is Nothing Then
                CType(connection, IDisposable).Dispose()
            End If
        End Try
    End Sub

    Private Sub getMajor()
        'SqlDataReader that will hold the returned results		
        Dim dr As SqlClient.SqlDataReader = Nothing

        'SqlConnection that will be used to execute the sql commands
        Dim connection As SqlClient.SqlConnection = Nothing

        Try
            Try
                connection = GetConnection()
            Catch ex As Exception
                MessageBox.Show("The connection with the database can't be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try

            Dim dsMajor As New DataSet
            dsMajor = SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, "sGetMajor", Nothing)

            Me.CmbMajor.DataSource = dsMajor.Tables(0)
            Me.CmbMajor.DisplayMember = "MajorDes"
            Me.CmbMajor.ValueMember = "MAJOR_CODE"

        Catch ex As Exception
            Dim errMessage As String = ""
            Dim tempException As Exception = ex

            While (Not tempException Is Nothing)
                errMessage += tempException.Message + Environment.NewLine + Environment.NewLine
                tempException = tempException.InnerException
            End While

            MessageBox.Show(String.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}" _
              + Environment.NewLine + "This test requires some modifications to the Transfer database.", errMessage), _
              "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not dr Is Nothing Then
                CType(dr, IDisposable).Dispose()
            End If

            If Not connection Is Nothing Then
                CType(connection, IDisposable).Dispose()
            End If
        End Try
    End Sub

    Private Sub CmbFac_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbFac.SelectedIndexChanged

    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        'Me.txtCoursno.Text = ""
        'Me.txtCredit_unit.Text = ""
        'Me.txtGrade.Text = ""
        'Me.txtCoursno.Focus()
        'Me.bindingSource1.AddNew()
        ''Me.txtId.Text = Me.Id
        'Me.btnAddNew.Enabled = False
    End Sub
End Class